package com.chinaoryx;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: hyman.zhang
 * Date: 12-1-27
 * Time: 下午7:49
 * To change this template use File | Settings | File Templates.
 */
public class ThreadFactory {
    private static ThreadFactory factory = new ThreadFactory();

    private List<TaskObject> taskList = new ArrayList<TaskObject>();

    private ThreadFactory() {
//        ExecutorService es = Executors.newCachedThreadPool();
//        Future future = null;

        Thread thread = null;
        MyThread myThread = null;
        try {
            TaskObject tObject = new TaskObject();
            tObject.setTaskId("task1");
            tObject.setParameterMap(null);
            tObject.setInterval(5);
            tObject.setTaskClassName("com.chinaoryx.MyTestTask");

            tObject.newThread();

//        future = es.submit(thread);
//        tObject.setFuture(future);
            taskList.add(tObject);


            tObject = new TaskObject();
            tObject.setTaskId("task2");
            tObject.setParameterMap(null);
            tObject.setInterval(6);
            tObject.setTaskClassName("com.chinaoryx.MyTestTask");
            tObject.newThread();
//        future = es.submit(thread);
//        tObject.setFuture(future);
            taskList.add(tObject);


            tObject = new TaskObject();
            tObject.setTaskId("task3");
            tObject.setParameterMap(null);
            tObject.setInterval(5);
            tObject.setTaskClassName("com.chinaoryx.MyTestTask");
            tObject.newThread();
//        future = es.submit(thread);
//        tObject.setFuture(future);
            taskList.add(tObject);


            tObject = new TaskObject();
            tObject.setTaskId("task4");
            tObject.setParameterMap(null);
            tObject.setInterval(5);
            tObject.setTaskClassName("com.chinaoryx.MyTestTask");
            tObject.newThread();
//        future = es.submit(thread);
//        tObject.setFuture(future);
            taskList.add(tObject);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (InstantiationException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (IllegalAccessException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }


    }

    public static ThreadFactory newInstance() {
        return factory;
    }


    public List<TaskObject> getTaskList() {
        return taskList;
    }

    public TaskObject getTaskObject(String taskId) {
        for (TaskObject task : this.taskList) {
            if (task.getTaskId().equals(taskId)) {
                return task;
            }
        }
        return null;
    }
}
